load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg/sql/colexecop:EXECGEN.bzl", "eg_go_filegroup", "gen_eg_go_rules")
load(":COLEXEC.bzl", "gen_sort_partitioner_rule")
load("//pkg/testutils:buildutil/buildutil.bzl", "disallowed_imports_test")

go_library(
    name = "colexec",
    srcs = [
        "aggregators_util.go",
        "buffer.go",
        "builtin_funcs.go",
        "case.go",
        "columnarizer.go",
        "constants.go",
        "count.go",
        "hash_aggregator.go",
        "hash_group_joiner.go",
        "insert.go",
        "invariants_checker.go",
        "limit.go",
        "materializer.go",
        "not_expr_ops.go",
        "offset.go",
        "ordered_aggregator.go",
        "parallel_unordered_synchronizer.go",
        "partially_ordered_distinct.go",
        "range_stats.go",
        "serial_unordered_synchronizer.go",
        "sort.go",
        "sort_chunks.go",
        "sort_utils.go",
        "sorttopk.go",
        "tuple_proj_op.go",
        "unordered_distinct.go",
        "values.go",
        ":gen-exec",  # keep
        ":gen-sort-partitioner",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/colexec",
    visibility = ["//visibility:public"],
    # Pin the dependencies used in auto-generated code.
    deps = [
        "//pkg/col/coldata",
        "//pkg/col/coldataext",  # keep
        "//pkg/col/typeconv",  # keep
        "//pkg/kv/kvserver/kvserverbase",
        "//pkg/roachpb",
        "//pkg/server/telemetry",  # keep
        "//pkg/settings",
        "//pkg/sql/catalog",
        "//pkg/sql/catalog/catenumpb",
        "//pkg/sql/catalog/colinfo",  # keep
        "//pkg/sql/catalog/descpb",
        "//pkg/sql/colconv",
        "//pkg/sql/colenc",
        "//pkg/sql/colexec/colexecagg",  # keep
        "//pkg/sql/colexec/colexecargs",
        "//pkg/sql/colexec/colexecbase",
        "//pkg/sql/colexec/colexeccmp",  # keep
        "//pkg/sql/colexec/colexechash",  # keep
        "//pkg/sql/colexec/colexecutils",
        "//pkg/sql/colexec/execgen",  # keep
        "//pkg/sql/colexecerror",
        "//pkg/sql/colexecop",
        "//pkg/sql/colmem",
        "//pkg/sql/execinfra",
        "//pkg/sql/execinfra/execopnode",
        "//pkg/sql/execinfra/execreleasable",
        "//pkg/sql/execinfrapb",
        "//pkg/sql/memsize",
        "//pkg/sql/row",
        "//pkg/sql/rowenc",
        "//pkg/sql/sem/catid",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/sqltelemetry",  # keep
        "//pkg/sql/types",
        "//pkg/util/buildutil",
        "//pkg/util/duration",  # keep
        "//pkg/util/encoding",  # keep
        "//pkg/util/intsets",
        "//pkg/util/json",  # keep
        "//pkg/util/log",
        "//pkg/util/metamorphic",
        "//pkg/util/mon",
        "//pkg/util/stringarena",
        "//pkg/util/tracing",
        "@com_github_cockroachdb_apd_v3//:apd",  # keep
        "@com_github_cockroachdb_errors//:errors",
    ],
)

go_test(
    name = "colexec_test",
    size = "large",
    srcs = [
        "aggregators_test.go",
        "and_or_projection_test.go",
        "buffer_test.go",
        "builtin_funcs_test.go",
        "case_test.go",
        "coalesce_test.go",
        "columnarizer_test.go",
        "count_test.go",
        "crossjoiner_test.go",
        "default_agg_test.go",
        "distinct_test.go",
        "external_distinct_test.go",
        "external_hash_aggregator_test.go",
        "external_hash_joiner_test.go",
        "external_sort_test.go",
        "hash_aggregator_test.go",
        "hash_group_joiner_test.go",
        "hashjoiner_test.go",
        "if_expr_test.go",
        "inject_setup_test.go",
        "is_null_ops_test.go",
        "joiner_utils_test.go",
        "limit_test.go",
        "main_test.go",
        "materializer_test.go",
        "mergejoiner_test.go",
        "not_expr_ops_test.go",
        "offset_test.go",
        "ordered_synchronizer_test.go",
        "parallel_unordered_synchronizer_test.go",
        "proj_utils_test.go",
        "rowtovec_test.go",
        "select_in_test.go",
        "serial_unordered_synchronizer_test.go",
        "sort_chunks_test.go",
        "sort_test.go",
        "sort_utils_test.go",
        "sorttopk_test.go",
        "types_integration_test.go",
        "values_test.go",
    ],
    embed = [":colexec"],
    shard_count = 16,
    deps = [
        "//pkg/col/coldata",
        "//pkg/col/coldataext",
        "//pkg/col/coldatatestutils",
        "//pkg/col/colserde",
        "//pkg/col/typeconv",
        "//pkg/settings/cluster",
        "//pkg/sql/catalog/colinfo",
        "//pkg/sql/catalog/descpb",
        "//pkg/sql/colcontainer",
        "//pkg/sql/colconv",
        "//pkg/sql/colexec/colbuilder",
        "//pkg/sql/colexec/colexecagg",
        "//pkg/sql/colexec/colexecargs",
        "//pkg/sql/colexec/colexecbase",
        "//pkg/sql/colexec/colexecjoin",
        "//pkg/sql/colexec/colexectestutils",
        "//pkg/sql/colexec/colexecutils",
        "//pkg/sql/colexecerror",
        "//pkg/sql/colexecop",
        "//pkg/sql/colmem",
        "//pkg/sql/execinfra",
        "//pkg/sql/execinfrapb",
        "//pkg/sql/memsize",
        "//pkg/sql/parser",
        "//pkg/sql/randgen",
        "//pkg/sql/rowenc",
        "//pkg/sql/rowexec",
        "//pkg/sql/sem/builtins",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/types",
        "//pkg/testutils",
        "//pkg/testutils/colcontainerutils",
        "//pkg/testutils/distsqlutils",
        "//pkg/testutils/skip",
        "//pkg/util/duration",
        "//pkg/util/encoding",
        "//pkg/util/humanizeutil",
        "//pkg/util/json",
        "//pkg/util/leaktest",
        "//pkg/util/log",
        "//pkg/util/mon",
        "//pkg/util/randutil",
        "//pkg/util/timeofday",
        "//pkg/util/uuid",
        "@com_github_apache_arrow_go_arrow//array",
        "@com_github_cockroachdb_apd_v3//:apd",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_marusama_semaphore//:semaphore",
        "@com_github_stretchr_testify//require",
    ],
)

# Map between target name and relevant template.
targets = [
    ("and_or_projection.eg.go", "and_or_projection_tmpl.go"),
    ("hash_aggregator.eg.go", "hash_aggregator_tmpl.go"),
    ("is_null_ops.eg.go", "is_null_ops_tmpl.go"),
    ("ordered_synchronizer.eg.go", "ordered_synchronizer_tmpl.go"),
    ("pdqsort.eg.go", "pdqsort_tmpl.go"),
    ("rowtovec.eg.go", "rowtovec_tmpl.go"),
    ("select_in.eg.go", "select_in_tmpl.go"),
    ("sort.eg.go", "sort_tmpl.go"),
    ("sorttopk.eg.go", "sorttopk_tmpl.go"),
    ("substring.eg.go", "substring_tmpl.go"),
    ("values_differ.eg.go", "values_differ_tmpl.go"),
    ("vec_comparators.eg.go", "vec_comparators_tmpl.go"),
]

# Define a file group for all the .eg.go targets.
eg_go_filegroup(
    name = "gen-exec",
    targets = targets,
)

# Define gen rules for individual eg.go files.
gen_eg_go_rules(targets)

# Special-case the gen rule for sort_partitioner.eg.go.
gen_sort_partitioner_rule(
    name = "gen-sort-partitioner",
    target = "sort_partitioner.eg.go",
)

disallowed_imports_test(
    "colexec",
    [
        "//pkg/sql/colexec/colexecjoin",
        "//pkg/sql/colflow",
        "//pkg/sql/rowexec",
        "//pkg/sql/rowflow",
    ],
)
